﻿using RuYiAdmin.Net.Common.Global;
using RuYiAdmin.Net.Entity.BusinessDTO.SystemManagement;
using RuYiAdmin.Net.Entity.BusinessEntity.SystemManagement;
using RuYiAdmin.Net.Entity.CoreEntity;
using RuYiAdmin.Net.Entity.CoreEnum;
using RuYiAdmin.Net.Repository.BusinessRepository.SystemManagement.CodeTable;
using RuYiAdmin.Net.Service.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace RuYiAdmin.Net.Service.BusinessService.SystemManagement.CodeTable
{
    /// <summary>
    /// 码表业务层实现
    /// </summary>
    class CodeTableService : BaseService<SysCodeTable>, ICodeTableService
    {
        /// <summary>
        /// 码表仓储实例
        /// </summary>
        private readonly ICodeTableRepository codeTableRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="codeTableRepository"></param>
        public CodeTableService(ICodeTableRepository codeTableRepository) : base(codeTableRepository)
        {
            this.codeTableRepository = codeTableRepository;
        }

        #region 公有方法

        /// <summary>
        /// 获取码表树
        /// </summary>
        /// <returns>ActionResult</returns>
        public QueryResult<SysCodeTableDTO> GetCodeTreeNodes()
        {
            var sqlKey = "sqls:sql:query_syscodetable";

            var strSQL = GlobalContext.Configuration.GetSection(sqlKey).Value;

            int totalCount = 0;

            var rootList = this.codeTableRepository.
                SqlQuery<SysCodeTableDTO>(new QueryCondition(), ref totalCount, strSQL).ToList();

            this.GetNodeChildren(rootList, sqlKey);

            var queryResult = new QueryResult<SysCodeTableDTO>();

            queryResult.HttpStatusCode = HttpStatusCode.OK;
            queryResult.Message = new String("OK");
            queryResult.List = new List<SysCodeTableDTO>();
            queryResult.List.AddRange(rootList);

            return queryResult;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 递归树
        /// </summary>
        /// <param name="rootList">根节点</param>
        /// <param name="sqlKey">SQL键值</param>
        private void GetNodeChildren(List<SysCodeTableDTO> rootList, String sqlKey)
        {
            foreach (var root in rootList)
            {
                var queryCondition = new QueryCondition();
                queryCondition.QueryItems = new List<QueryItem>();
                queryCondition.QueryItems.Add(new QueryItem()
                {
                    Field = "ParentId",
                    DataType = DataType.Guid,
                    QueryMethod = QueryMethod.Equal,
                    Value = root.Id
                });

                var strSQL = GlobalContext.Configuration.GetSection(sqlKey).Value.Replace("AND isnull(ParentId)=1", "");

                int totalCount = 0;

                var list = this.codeTableRepository.SqlQuery<SysCodeTableDTO>(queryCondition, ref totalCount, strSQL);

                if (list.Count > 0)
                {
                    root.Children = new List<SysCodeTableDTO>();
                    root.Children.AddRange(list.OrderBy(t => t.Order).ToList());

                    this.GetNodeChildren(root.Children, sqlKey);
                }
            }
        }

        #endregion
    }
}
